package com.cytech.cloudclinic.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.cytech.cloudclinic.R;
import com.cytech.cloudclinic.base.BaseBean;
import com.cytech.cloudclinic.base.BaseObserver;
import com.cytech.cloudclinic.bean.event.UpdateAddTagEvent;
import com.cytech.cloudclinic.moudle.main.drug.add.dosage.AddDrugDosageView;
import com.cytech.cloudclinic.utils.RequestWithViewUtils;
import com.yechaoa.yutils.ToastUtil;
import com.yechaoa.yutils.YUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import static com.cytech.cloudclinic.app.Constant.MODE_PACKAGE_ID;

public class AddDrugTagDialog extends DialogFragment {

    @BindView(R.id.dialog_add_drug_tag_value)
    EditText etValue;
    @BindView(R.id.dialog_add_drug_tag_save)
    TextView btnFSave;

    private int modeId = MODE_PACKAGE_ID;
    private int drugId = 0;

    public AddDrugTagDialog(int modeId,int drugId) {
        Bundle bundle = new Bundle();
        bundle.putInt("modeId",modeId);
        bundle.putInt("drugId",drugId);
        setArguments(bundle);
    }

    @Override
    public void onStart() {
        super.onStart();
        WindowManager.LayoutParams lp = getDialog().getWindow().getAttributes();
        lp.width = (int) (0.5* YUtils.getScreenWidth());
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getDialog().getWindow().setAttributes(lp);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        modeId = (int) getArguments().get("modeId");
        drugId = (int) getArguments().get("drugId");

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_add_drug_tag, null);
        ButterKnife.bind(this, view);
        builder.setView(view);
        return builder.create();
    }

    @OnClick({R.id.dialog_add_drug_tag_save})
    public void saveTag(View view) {
        String value = etValue.getText().toString();
        if(TextUtils.isEmpty(value)){
            ToastUtil.showToast("请输入名称");
            return;
        }
        JSONObject json = new JSONObject();
        try {
            json.put("mode",modeId);
            json.put("drug_tag",drugId);
            json.put("value",value);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        String requestStr = json.toString();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), requestStr);
        RequestWithViewUtils.getInstance().addDisposable(RequestWithViewUtils.getInstance().apiServer.addSummaryTag(body), new BaseObserver<BaseBean<Object>>(null, false) {
            @Override
            public void onSuccess(BaseBean<Object> o) {
               dismiss();
               EventBus.getDefault().post(new UpdateAddTagEvent(modeId));
            }

            @Override
            public void onError(String msg) {

            }
        });
    }
}
